package com.wtb.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wtb.dao.QqDao;
import com.wtb.pub.WtbException;
import com.wtb.qq.domain.QqDetailInfo;
import com.wtb.qq.domain.QqOauth;
import com.wtb.rr.domain.RrOauth;
import com.wtb.utils.JdbcUtils;

public class QqDaoImpl implements QqDao {
	private static final Log log = LogFactory.getLog(QqDaoImpl.class);
	public void insert(QqOauth qqOauth) {

	}
	public void insert(QqDetailInfo qqDetailInfo) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "insert into qqdata(uni,oauth_token,oauth_token_secrete,createTime,ref,q2r,q2s,email,nick,location) values (?,?,?,?,?,?,?,?,?,?)";
			Object[] params = {qqDetailInfo.getUni(),qqDetailInfo.getOauth_token(),qqDetailInfo.getOauth_token_secrete(),qqDetailInfo.getCreateTime(),qqDetailInfo.getRef(),qqDetailInfo.getQ2r(),qqDetailInfo.getQ2s(),qqDetailInfo.getEmail(),qqDetailInfo.getNick(),qqDetailInfo.getLocation()};
			qr.update(JdbcUtils.getConnection(), sql, params);
		} catch (SQLException e) {
			log.error("插入qqdata错误：", e);
			throw new WtbException("插入qqdata错误",e);
		}
		
	}
	
	public void del(QqOauth qqOauth) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "delete from qqdata where uni=?";
			qr.update(JdbcUtils.getConnection(), sql, qqOauth.getUni());
		} catch (SQLException e) {
			log.error("删除qqdata错误：", e);
			throw new WtbException("删除qqdata错误"+e);
		}

	}

	public void update(QqOauth qqOauth) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "update qqdata set oauth_token=?,oauth_token_secrete=?,createTime=?,ref=?,q2r=? where uni=?";
			Object[] params = {qqOauth.getOauth_token(),qqOauth.getOauth_token_secrete(),qqOauth.getCreateTime(),qqOauth.getRef(),qqOauth.getQ2r(),qqOauth.getUni()};
			qr.update(JdbcUtils.getConnection(), sql, params);
		} catch (SQLException e) {
			log.error("更新qqdata错误：", e);
			throw new WtbException("更新qqdata错误"+e);
		}
	}
	
	public void update(QqDetailInfo qqDetailInfo) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "update qqdata set email=?,nick=?,location=? where uni=?";
			Object[] params = {qqDetailInfo.getEmail(),qqDetailInfo.getNick(),qqDetailInfo.getLocation(),qqDetailInfo.getUni()};
			qr.update(JdbcUtils.getConnection(), sql, params);
		} catch (SQLException e) {
			log.error("更新qqdata错误：", e);
			throw new WtbException("更新qqdata错误"+e);
		}
	}
	@SuppressWarnings("deprecation")
	public QqOauth findByQq(QqOauth qqOauth) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "select * from qqdata where uni=?";
			qqOauth = (QqOauth) qr.query(JdbcUtils.getConnection(), sql,qqOauth.getUni(),new BeanHandler(QqOauth.class));
			return qqOauth;
		} catch (SQLException e) {
			log.error("通过qqoauth查找qqdata错误：", e);
			throw new WtbException("通过qqoauth查找qqdata错误"+e);
		}
	}

	@SuppressWarnings("deprecation")
	public QqOauth findByRr(RrOauth rrOauth) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "select * from qqdata where ref=?";
			QqOauth qqOauth = (QqOauth) qr.query(JdbcUtils.getConnection(), sql,rrOauth.getUni(),new BeanHandler(QqOauth.class));
			return qqOauth;
		} catch (SQLException e) {
			log.error("通过rroauth查找qqdata错误：", e);
			throw new WtbException("通过rroauth查找qqdata错误"+e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<QqOauth> getAll() {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "select * from qqdata";
			return (List<QqOauth>) qr.query(JdbcUtils.getConnection(), sql,new BeanListHandler(QqOauth.class));
		} catch (SQLException e) {
			log.error("查询qqdata所有信息：", e);
			throw new WtbException("查询qqdata所有信息"+e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<QqDetailInfo> getAllInfo() {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "select * from qqdata";
			return (List<QqDetailInfo>) qr.query(JdbcUtils.getConnection(), sql,new BeanListHandler(QqDetailInfo.class));
		} catch (SQLException e) {
			log.error("查询qqdata所有信息：", e);
			throw new WtbException("查询qqdata所有信息"+e);
		}
	}

	

}
